<?php 
namespace Home\Controller;
use Think\Controller;
class UserController extends Controller{
	public function all(){
		// $rid = $this->sys_user_roleid;
		// $user_id = $this->sys_userid;
		$rid = I('rid');
		$user_id = I('uid');
		$rid = I('id');
		$param = ['id'=>'id','name'=>'role_name'];
		$underling = $this->getRoleAuth($rid,$param,'name','id');
		$fields = [
			'id'=>'id',
			'name'=>'username',
			'role'=>'role_id',
			'status'=>'status'
		];
		$array_map = [
			'role'=>$underling,
			'status'=>['失效','生效']
		];
		// 角色上下级过滤
		$user_list_o = D('TUser')->fields($fields)->useValueMap($array_map)->getUserList(array_keys($underling));
		// 原数组结构不易满足下面验证，做修改
		foreach ($user_list_o as $value) {
			$user_list[$value['id']]=$value;
		}
		// 省份从属关系过滤
		// $province = D('UserProvince')->getUserProvince($user_id);
		// $user_list = D('UserProvince')->provinceFilter($user_list,$province);
		// $rdata = $user_list?returnData($user_list):returnData();
		// $this->ajaxReturn($rdata);
	}

	public function role(){
		// $rid = $this->sys_user_roleid;
		$rid = I('rid');
		$underling = $this->getRoleAuth($rid,'','role_name','id');
		foreach ($underling as $key => $value) {
			$data[] = ['id'=>$key,'role_name'=>$value];
		}
		// $province = D('UserProvince')->getUserProvince($user_id);
		// $rdata = returnData(200,['role_group'=>$underling,'status'=>]);
		$this->ajaxReturn($data);
	}

	public function addInfo(){
		$name = I('post.name');
		$status = I('post.status')==1?1:0;
		$role_id = I('post.role_id');
		$province = I('province');
		$rid = $this->sys_user_roleid;
		$underling = $this->getRoleAuth($rid,'','role_name','id');
		// 分组验证
		if( !in_array($role_id, array_keys($underling)) ){
			$rdata = returnData(403,$data);
		}else{
			$time = time();
			$info = [
				'username'=>$name,
				'password'=>md5('123456'),
				'status'=>$status,
				'role_id'=>$role_id,
				'create_time'=>$time,
				'recent_time'=>$time,
			];
			D('TUser')->add($info);
			$rdata = returnData(200,$data);
		}
		$this->ajaxReturn($rdata);
	}

	public function edit(){
		$id = I('id',-1);
		$rid = $this->sys_user_roleid;
		$rid = I('rid',-1);
		$underling = $this->getRoleAuth($rid,'','role_name','id');
		$fields = ['id'=>'id','name'=>'username','role_id'=>'role_id','status'=>'status'];
		$info = D('TUser')->fields($fields)->checkAuth(array_keys($underling))->getInfo($id);
		$data = array_merge($info,['role_group'=>$underling,'status_group'=>['失效','生效']]);
		$rdata = $info?returnData(200,$data):returnData(403);
		$this->ajaxReturn($rdata);
	}

	public function editInfo(){
		$data = ['url'=>'http://202.97.183.149:6002/user/all'];
		$id = I('post.id');
		$name = I('post.name');
		$status = I('post.status')==1?1:0;
		$role_id = I('post.role_id');
		$rid = $this->sys_user_roleid;
		$underling = $this->getRoleAuth($rid,'','role_name','id');
		$info = D('TUser')->checkAuth(array_keys($underling))->getInfo($id);
		// 用户验证及分组验证
		if( !$info || !in_array($role_id, array_keys($underling)) ){
			$rdata = returnData(403,$data);
		}else{
			$info = ['id'=>$id,'status'=>$status,'role_id'=>$role_id,'username'=>$name];
			D('TUser')->save($info);
			$rdata = returnData(200,$data);
		}
		$this->ajaxReturn($rdata);
	}

	public function del(){
		$id = I('post.id',-1);
		$rid = $this->sys_user_roleid;
		$underling = $this->getRoleAuth($rid,'','role_name','id');
		$info = D('TUser')->checkAuth(array_keys($underling))->getInfo($id);
		// 用户验证
		if( !$info ){
			$rdata = returnData(403,$data);
		}else{
			D('TUser')->where(['id'=>$id])->delete();
			$rdata = returnData(200,$data);
		}
		$this->ajaxReturn($rdata);
	}

	private function getRoleAuth($role_id,$param,$value,$key=''){
		$role = D('TRole')->setRoleId($role_id)->getRolerArrayByFields($param);
		return twoToOne($role,$value,$key);
	}

	public function passwd(){
		$oldpwd = I('post.oldpwd');
		$newpwd = I('post.newpwd');
		$rnewpwd = I('post.rnewpwd');
		$rdata = $this->editpwd($oldpwd,$newpwd,$rnewpwd)?returnData(200):returnData(403);
		$this->ajaxReturn($rdata);
	}

	private function editpwd($o,$n,$r){
		if( !$n || $n!=$r ){
			return false;
		}
		$pwd = D('TUser')->where(['id'=>$this->sys_userid])->getField('password');
		if( md5($o) != $pwd ){
			return false;
		}
		$data = [
			'password' => md5($n)
		];
		D('TUser')->where(['id'=>$this->sys_userid])->save($data);
		return true;
	}
}